//
//  CZGoodWorkViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/12.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZGoodWorkViewController.h"
#import "CZGoodWorkCell.h"
#import "CZWebNoticeViewController.h"

static NSString * const good_work_cell = @"good_work_cell";

@interface CZGoodWorkViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *goodWorkTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) LGRefreshView *refreshView;
@end

@implementation CZGoodWorkViewController

- (NSMutableArray *__nonnull)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentPage = 1;
    [self.goodWorkTableView registerNib:[UINib nibWithNibName:@"CZGoodWorkCell" bundle:nil] forCellReuseIdentifier:good_work_cell];
    self.goodWorkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.goodWorkTableView.estimatedRowHeight = 100;
    [self postGoodWordData];
    
    _refreshView = [LGRefreshView refreshViewWithScrollView:self.goodWorkTableView
                                             refreshHandler:^(LGRefreshView *refreshView){
                                                 [self.goodWorkTableView.mj_footer setState:(MJRefreshStateIdle)];
                                                 _currentPage = 1;
                                                 [self postGoodWordData];
                                             }];
    _refreshView.center = CGPointMake(Main_Screen_Width / 2, _refreshView.center.y);
    _refreshView.tintColor = ThemeColor;
    _refreshView.backgroundColor = [UIColor clearColor];
    
    self.goodWorkTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self postGoodWordData];
    }];
    self.goodWorkTableView.mj_footer.hidden = YES;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark- 获取优秀作品
- (void)postGoodWordData {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/article/index",APP_HOST];
    [SVProgressHUD show];
    [GCZAFNetWorking POST:urlString params:@{@"article_type" : @2, @"page" : @(_currentPage)} success:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"] isEqualToString:@"200"]) {
            NSArray *arr = response[@"result"];
            if (!arr || arr.count < 1) {
                [self.goodWorkTableView.mj_footer endRefreshingWithNoMoreData];
                return ;
            }
            if (_currentPage == 1) {
                self.dataArray = arr.mutableCopy;
            } else {
                for (NSDictionary *dic in arr) {
                    [self.dataArray addObject:dic];
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.goodWorkTableView reloadData];
                self.goodWorkTableView.mj_footer.hidden = NO;
            });
        }
        [_refreshView endRefreshing];
        [self.goodWorkTableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [self.goodWorkTableView.mj_footer endRefreshing];
        [_refreshView endRefreshing];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZGoodWorkCell *cell = [tableView dequeueReusableCellWithIdentifier:good_work_cell];
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.authorLabel.text = [NSString stringWithFormat:@"作者：%@" ,dic[@"author"]];
    cell.sharePicImageView.yy_imageURL = dic[@"cover_pic"];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", dic[@"title"]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataArray[indexPath.row];
    CZWebNoticeViewController *webVC = [[CZWebNoticeViewController alloc] init];
    webVC.type = @"优秀作品";
//    webVC.urlString = dic[@"article_link"];
    webVC.articleID = dic[@"article_id"];
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
